package se.peterbjorkman.android.trafikkamera;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CameraView extends LinearLayout {

    private TextView mDescription;
    private TextView mModifiedTime;
    private ImageView mImageView;
    private ProgressBar mProgressBar;
    private LinearLayout mProgressLayout;
    private TextView mProgressTextView;
    private int mDone = 0;
    private int mTotal = 0;
    private String mStatus = "";
    
    public CameraView(Context context) {
        super(context);

        setOrientation(VERTICAL);        

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.camerapictureview, this);
        
        mDescription = (TextView)findViewById(R.id.TextViewDescription);
       
        mModifiedTime = (TextView)findViewById(R.id.TextViewModifiedTime);
        mModifiedTime.setText("");
        mImageView = (ImageView)findViewById(R.id.ImageView);
        mProgressBar = (ProgressBar)findViewById(R.id.ProgressBar);
        mProgressLayout = (LinearLayout)findViewById(R.id.LinearLayoutProgress);
        mProgressTextView = (TextView)findViewById(R.id.ProgressTextView);
    }

    public void setDescription(String title) {
        mDescription.setText(title);
    }

    public void setModifiedText(String modifiedTime) {
        mModifiedTime.setText(modifiedTime);
    }
    
    public void setImage(Drawable image) {
    	mImageView.setImageDrawable(image);
    }
    
    public void hideAllProgressIndication(){
    	mProgressLayout.setVisibility(View.GONE);	
    }
    
    public void setProgressStatus(String status){
    	mStatus = status;
    	mProgressLayout.setVisibility(View.VISIBLE);
    	updateStatusText();
    }

	private void updateStatusText() {
		String text = mStatus;
		if (mDone > 0){
			text = text + " " + mDone;
			if (mTotal < 0)
				text = text + "/" + mTotal;	
		}
		
		mProgressTextView.setText(text);
	}
    
    public void setProgress(int done, int total){
    	mTotal = total;
    	mDone = done;
    	mProgressLayout.setVisibility(View.VISIBLE);
    	mProgressBar.setVisibility(View.VISIBLE);
    	if (total > 0){
    		mProgressBar.setIndeterminate(false);
    		mProgressBar.setProgress(calculateProgress(done, total));
    	} else {
    		mProgressBar.setIndeterminate(true);
    	}
    	updateStatusText();
    }
    
	private static int calculateProgress(int done, int total) {
		return (int)(((double)done*100.0F) / ((double)total));
	}

	public Drawable getDrawable() {
		return mImageView.getDrawable();
	}

	public String getModifiedText() {
		return mModifiedTime.getText().toString();
	}
}